package com.hnyfkj.jyindustry.biz.kehu.controller;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;


import com.hnyfkj.jyindustry.biz.kehu.dto.UserInsertDto;
import com.hnyfkj.jyindustry.biz.kehu.dto.UserQueryDto;
import com.hnyfkj.jyindustry.biz.kehu.dto.UserUpdateDto;
import com.hnyfkj.jyindustry.biz.kehu.entity.User;
import com.hnyfkj.jyindustry.biz.kehu.service.UserService;
import com.hnyfkj.jyindustry.common.dto.PageReturnDto;
import com.hnyfkj.jyindustry.common.dto.RespDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;


/**
 * 理发店-------客户表
 *
 * @author zhangzhang
 * @email 15838129129@163.com
 * @date 2021-04-18 16:08:50
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 添加页面
     */
    @GetMapping("/add")
    public String addPage() {
        return "user/user-add";
    }

    /**
     * 编辑页面
     */
    @GetMapping("/edit")
    public String editPage(@RequestParam("id") String id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "user/user-edit";
    }


    /**
     * 续费页面
     */
    @GetMapping("/xufei")
    public String xufei(@RequestParam("id") String id, Model model) {
        model.addAttribute("id", id);
        return "user/user-xufei";
    }

    /**
     * 变更页面
     */
    @GetMapping("/biangeng")
    public String biangeng(@RequestParam("id") String id, Model model) {
        User user = userService.getById(id);

        model.addAttribute("id", id);
        model.addAttribute("memberRank", user.getMemberRank());
        return "user/user-biangeng";
    }



    /**
     * 时间页面
     */
    @GetMapping("/timeUpdate")
    public String timeUpdate(@RequestParam("id") String id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("id", id);
        model.addAttribute("user", user);
        return "user/user-date";
    }


    /**
     * 列表页面
     */
    @GetMapping("/listPage")
    public String listPage() {
        return "user/user-table";
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public RespDto<PageReturnDto<User>> list(@RequestBody UserQueryDto queryDto) {
        return RespDto.succResult(userService.queryPage(queryDto));
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public RespDto<User> info(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        return RespDto.succResult(user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public RespDto<String> save(@RequestBody UserInsertDto userInsertDto) {
        userService.insert(userInsertDto);
        return RespDto.succ();
    }

    /**
     * 续费
     */
    @RequestMapping("/xuFeiYear")
    @ResponseBody
    public RespDto<String> xuFeiYear(@RequestBody UserUpdateDto userupdateDto) {
        User user = userService.getById(userupdateDto.getId());
        if (userupdateDto.getRenewYearNum() != null && userupdateDto.getRenewYearNum() != 0) {
            //增加N年
            //判断时间是否超过
            Date time = new Date();
            if (user.getEffectTime().compareTo(time) > 0) {
                //提前续期
                Calendar cal = Calendar.getInstance();
                cal.setTime(user.getEffectTime());   //设置当前时间
                cal.add(Calendar.YEAR, userupdateDto.getRenewYearNum());  //在当前时间基础上加一年
                user.setEffectTime(cal.getTime());
            } else {
                //超时续期
                Calendar cal = Calendar.getInstance();
                cal.setTime(time);   //设置当前时间
                cal.add(Calendar.YEAR, userupdateDto.getRenewYearNum());  //在当前时间基础上加一年
                user.setEffectTime(cal.getTime());
            }
        }
        userService.updateById(user);
        return RespDto.succ();
    }

    /**
     * 续费
     */
    @RequestMapping("/changeType")
    @ResponseBody
    public RespDto<String> changeType(@RequestBody UserUpdateDto userupdateDto) {
        User user = userService.getById(userupdateDto.getId());
        user.setMemberRank(userupdateDto.getMemberRank());
        userService.updateById(user);
        return RespDto.succ();
    }

    /**
     * 时间
     */
    @RequestMapping("/changeTimeUpdate")
    @ResponseBody
    public RespDto<String> changeTimeUpdate(@RequestBody UserUpdateDto userupdateDto) {
        User user = userService.getById(userupdateDto.getId());
        user.setEffectTime(userupdateDto.getEffectTime());
        user.setAddTime(userupdateDto.getAddTime());
        userService.updateById(user);
        return RespDto.succ();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public RespDto<String> update(@RequestBody UserUpdateDto userupdateDto) {
        userService.update(userupdateDto);
        return RespDto.succ();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public RespDto<String> delete(String ids) {
        System.out.println(ids);
        userService.delete(ids);
        return RespDto.succ();
    }

    /**
     * 剪发次数 -1
     */
    @DeleteMapping("/jianfa")
    @ResponseBody
    public RespDto<String> jianfa(String ids) {
        return userService.jianfa(ids);
    }


    /**
     * 干洗次数 -1
     */
    @DeleteMapping("/ganxi")
    @ResponseBody
    public RespDto<String> ganxi(String ids) {

        return userService.ganxi(ids);
    }

    /**
     * 洗发次数 -1
     */
    @DeleteMapping("/xifa")
    @ResponseBody
    public RespDto<String> xifa(String ids) {
        return userService.xifa(ids);
    }


}
